{
 "metadata": {
  "name": "",
  "signature": "sha256:f72b8b227692da7e7c24fa6ba782c0195246b7fe2c73a172e3d6fd82efbd84a3"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import socket\n",
      "import networkx as nx\n",
      "import sys,time\n",
      "import numpy as np\n",
      "import random\n",
      "import matplotlib.cm as cmx\n",
      "import math\n",
      "\n",
      "from sdt_draw_utils import draw_sdt_nx,draw_sdt_nx_nodes,draw_sdt_nx_edges,draw_sdt_nx_node_labels,draw_sdt_nx_edge_labels,sdt_nx_circular,sdt_nx_random,sdt_nx_spectral,sdt_nx_spring,sdt_nx_shell,set_status,pan"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 1
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "try:\n",
      "    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)\n",
      "except socket.error, msg:\n",
      "    print(\"[ERROR] %s\\n\" % msg[1])\n",
      "    sys.exit(1)\n",
      "# \n",
      "try:\n",
      "    sock.connect((\"127.0.0.1\", 50000))\n",
      "except socket.error, msg:\n",
      "    sys.stderr.write(\"[ERROR] %s\\n\" % msg[1])\n",
      "    sys.exit(2)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 2
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# Get some networkx graph\n",
      "#clear things    \n",
      "sock.sendall('clear all ')\n",
      "#    sock.sendall('clear nodes ')    \n",
      "sock.sendall('layer Worldwind,off ')\n",
      "sock.sendall('layer Sdt::Kml,off ')\n",
      "sock.sendall('layer \"All Layers::Sdt::Node Labels,off\" ')\n",
      "sock.sendall('backgroundColor black ')\n",
      "sock.sendall('origin 0.0,0.0,0.0 ')\n",
      "sock.sendall('center 0.0,0,0.0,0.0,c ')\n",
      "#    sock.sendall('flyto 0.005,0.005,3000.0 ')\n",
      "sock.sendall('flyto 0.005,0.005,3000.0 ')\n",
      "#sock.sendall(sdt_com)\n",
      "#    sock.sendall(\"follow all,on \") \n",
      "#    draw_sdt_nx(sock,G,node_size=20,alpha=0.7,edge_color=\"blue\",width=0.3)\n",
      "#Try a colormap\n",
      "sock.sendall('title \"A Protean Christmas\" ')\n",
      "sock.sendall('showSdtStatusPanel,off ')"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 3
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "colormap = cmx.spectral\n",
      "numnodes=500\n",
      "colors = []\n",
      "for i in range(numnodes):\n",
      "    colors.append(random.uniform(0.0,1.0))\n",
      "#Setup tree or cone surface\n",
      "height = 0.8\n",
      "radius = 0.4\n",
      "tangent = radius/height\n",
      "sl_height = math.sqrt(height*height+radius*radius)\n",
      "pos = []\n",
      "\n",
      "for i in range(numnodes):\n",
      "#generate a random 3d position on a cone\n",
      "# Steps: generate random height then random cirle position given height.\n",
      "#\n",
      "#        rh=random.betavariate(2,4)\n",
      "#        rh=np.random.lognormal(mean=0.3,sigma=0.2)\n",
      "    placed_on_tree = False\n",
      "    while not placed_on_tree:\n",
      "        rh= np.random.uniform(0,1)\n",
      "        rh= rh * height\n",
      "        top=height-rh\n",
      "        rradius = top*tangent\n",
      "    # Need more prob on placing in relationship to radius\n",
      "        prob = rradius/radius\n",
      "        if np.random.uniform(0,1) < prob:\n",
      "            placed_on_tree = True\n",
      "    radians = random.uniform(0.0,math.pi*2.0*1.0)\n",
      "    x = (math.sin(radians)*rradius)\n",
      "    y = (math.cos(radians)*rradius)\n",
      "    pos.append((x,y,rh))\n",
      "nodes = [x for x in range(numnodes)]\n",
      "G = nx.Graph()\n",
      "G.add_nodes_from(nodes)\n",
      "draw_sdt_nx_nodes(sock,G,pos,geoPos=False,\n",
      "            node_color=colors,\n",
      "            node_shape=\"sphere\",\n",
      "            node_size=20,\n",
      "            n_alpha=0.7,\n",
      "            cmap=colormap)\n",
      "H=nx.Graph()\n",
      "H.add_node(\"star\")\n",
      "starpos=[(0.0,0.0,height+0.05)]\n",
      "draw_sdt_nx_nodes(sock,H,starpos,geoPos=False,\n",
      "            node_color=\"yellow\",\n",
      "            node_shape=\"Sphere\",\n",
      "            node_size=60,\n",
      "            n_alpha=0.9)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 4
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "sock.sendall('flyto 0.000,0.00,4000.0 ')"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 5
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "sock.sendall('clear links ')\n",
      "G.remove_edges_from(G.edges())\n",
      "# make some edges\n",
      "limit = 0.07\n",
      "for n,n_pos in enumerate(pos):\n",
      "    for k, k_pos in enumerate(pos):\n",
      "        dist = np.sqrt((k_pos[0]-n_pos[0])**2.0 \n",
      "                           + (k_pos[1]-n_pos[1])**2.0 \n",
      "                           + (k_pos[2]-n_pos[2])**2.0)\n",
      "        if n != k and dist < limit:\n",
      "            G.add_edge(n,k)\n",
      "draw_sdt_nx_edges(sock,G,pos,geoPos=False,\n",
      "            edge_color=\"white\",\n",
      "            line_widths=1.0,\n",
      "            l_alpha=0.5,\n",
      "            style='solid')\n",
      "sock.sendall('collapseLinks on ')"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 8
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "on = True\n",
      "while True:\n",
      "    if on:\n",
      "        sock.sendall('layer \"All Layers::Sdt::Network Links,off\" ')\n",
      "        on=False\n",
      "    else:\n",
      "        sock.sendall('layer \"All Layers::Sdt::Network Links,on\" ')\n",
      "        on=True        \n",
      "    time.sleep(random.uniform(0.5,3.0))"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 11
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 11
    }
   ],
   "metadata": {}
  }
 ]
}